package com.dkcd.chenshiproject.tabHostActivity;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.dkcd.chenshiproject.MainActivity;
import com.dkcd.chenshiproject.R;
import com.dkcd.chenshiproject.activity.MusicPlayerActivity;
import com.dkcd.chenshiproject.adapter.MusicAdapter;
import com.dkcd.chenshiproject.entity.Song;
import com.dkcd.chenshiproject.service.MusicPlayerService;
import com.dkcd.chenshiproject.util.MusicUtils;
import com.dkcd.chenshiproject.util.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MusicPavilionFragment extends Fragment {
    private ListView listView;
    private AutoCompleteTextView musicPavilionSearch;
    private MusicAdapter myAdapter;
    private List<Song> musicData = MainActivity.musicData;
    public static Handler handlers;

    public MusicPavilionFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handlear();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.e("TAG", "onCreateView: fragment_index_map执行了");
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_music_pavilion, container, false);
        init(inflate);
        initView();
        musicSelectOnClick();
        return inflate;
    }

    @Override
    public void onStart() {
        super.onStart();
        initView();
    }

    private void init(View view){
        listView = (ListView) view.findViewById(R.id.music_pavilion_list);
        musicPavilionSearch = (AutoCompleteTextView)view.findViewById(R.id.music_pavilion_search);

    }

    private void initView(){
        Log.e("initView", "initView: MusicPavilionFragment 初始化" );
        //初始化 音乐馆 listview
        myAdapter = new MusicAdapter(getActivity(), musicData);
        //初始化 AutoCompleteTextView
        List<String> songsName = new ArrayList<>();
        for (Song musicDatum : musicData) {
            String song = musicDatum.song;
            songsName.add(song);
        }
        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(getContext(),
                android.R.layout.simple_dropdown_item_1line,songsName);
        musicPavilionSearch.setAdapter(stringArrayAdapter);
        Integer position = (Integer)SPUtils.getValue(getContext(), "position", 0);
        //获取默认选中的行
        if (position!=null){
            myAdapter.setSelectPosition(position);
        }
        listView.setAdapter(myAdapter);
        listView.setSelection(position);

    }

    /**
     *
     * @author ChenShi
     * @version：1.0
     * @date
     * 描述: 选择音乐 并播放
     *
     */
    private void musicSelectOnClick(){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                myAdapter.setSelectPosition(i);
                SPUtils.putValue(view.getContext(),"position",i);
                Intent intent = new Intent(getContext(), MusicPlayerActivity.class);
                Bundle bundle = new Bundle();
                Log.e("TAG", "onItemClick: "+i);
                //传入路径
                bundle.putString("musicPath",musicData.get(i).path);
                bundle.putString("maker","MusicPavilionFagment");
                //传入id
                bundle.putInt("musicId",i);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }


    /**
     *
     * @author ChenShi
     * @version：1.0
     * @date
     * 描述: 修改指定音乐颜色
     *
     */
    public void handlear(){
        handlers = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                switch (msg.what){
                    case MusicPlayerService.CHANGE_MUSIC_ID:{
                        Log.e("TAG", "handleMessage: 更换歌曲高亮了" );
                        int musicId = msg.getData().getInt("musicId");
                        SPUtils.putValue(getContext(), "position",musicId);
                        initView();
                    }break;
                }
                return false;
            }
        });
    }
}